package com.xueyi.aixihu.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.xueyi.aixihu.constant.*;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.entity.po.AxhFinanceLog;
import com.xueyi.aixihu.entity.vo.DataList;
import com.xueyi.aixihu.entity.vo.DtNewsVo;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.entity.vo.WaitAuditNewsVo;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.FinanceAuditMapper;
import com.xueyi.aixihu.mapper.NewAuditMapper;
import com.xueyi.aixihu.mapper.ReportFormsMapper;
import com.xueyi.aixihu.reqeust.AgreePassNewsAuditReq;
import com.xueyi.aixihu.reqeust.AuditNewsReq;
import com.xueyi.aixihu.reqeust.FindWaitAuditNewsListReq;
import com.xueyi.aixihu.reqeust.OpenNewsReq;
import com.xueyi.aixihu.service.NewAuditService;
import com.xueyi.aixihu.status.SystemCode;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hxs
 * @createDate 2023/6/28 10:05
 * @description 文章审核接口
 */
@Service
@AllArgsConstructor
public class NewAuditServiceImpl implements NewAuditService {

    @Resource(type = NewAuditMapper.class)
    private NewAuditMapper newAuditMapper;

    @Resource(type = ReportFormsMapper.class)
    private ReportFormsMapper reportFormsMapper;

    @Resource(type = FinanceAuditMapper.class)
    private final FinanceAuditMapper financeAuditMapper;

    /**
     * 查询待审核列表
     *
     * @return 文章列表
     */
    @Override
    public DataList<WaitAuditNewsVo> findWaitAuditNewsList(FindWaitAuditNewsListReq findWaitAuditNewsListReq) throws SzException {
        findWaitAuditNewsListReq.format();

        //获取当前登录用户
        DtUsersVo user = DtUsersVo.findCurrentUser();

        Integer role = user.getUserMaxRole();


        String areaIds = "";

        //获取审核类型
        Integer auditType = findWaitAuditNewsListReq.getAuditType();
        //文章镇级党务审核 由镇党建办主任审核
        if (AuditConstant.WZ_ZJ_DW_AUDIT.equals(auditType)) {
            if (!SystemRoleConstant.Z_DJB_ZR.equals(role)&&!SystemRoleConstant.SUPER_ADMIN.equals(role)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageTownId();
        }
        //文章镇级政务审核 由镇便民服务中心主任审核
        else if (AuditConstant.WZ_ZJ_ZW_AUDIT.equals(auditType)) {
            if (!SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role)&&!SystemRoleConstant.SUPER_ADMIN.equals(role)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageTownId();
        }
        //文章村级党务审核 由镇党建办成员和镇党建办主任审核
        else if (AuditConstant.WZ_CJ_DW_AUDIT.equals(auditType)) {
            if (!(SystemRoleConstant.Z_DJB_CY.equals(role) || SystemRoleConstant.Z_DJB_ZR.equals(role))) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageAreaId() + "," + user.findUserManageCommunityId();
        }
        //文章村级村务审核 由镇便民服务中心主任和镇相关部门审核
        else if (AuditConstant.WZ_CJ_CW_AUDIT.equals(auditType)) {
            if (!(SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role) || SystemRoleConstant.Z_XG_BM.equals(role))) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageAreaId() + "," + user.findUserManageCommunityId();
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }

        if (!org.springframework.util.StringUtils.hasLength(areaIds)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        Integer index = findWaitAuditNewsListReq.getCurrentStatus();
        if (index.equals(0)) {
            findWaitAuditNewsListReq.setCurrentStatusStr(OneLevelAuditConstant.SUBMIT_AUDIT + "");
        } else {
            findWaitAuditNewsListReq.setCurrentStatusStr(OneLevelAuditConstant.CONSENT + "," + OneLevelAuditConstant.FAIL);
        }


        Integer total = newAuditMapper.findWaitAuditNewsListTotal(findWaitAuditNewsListReq, areaIds);
        List<WaitAuditNewsVo> list = new ArrayList<>();
        if (total > 0) {
            list = newAuditMapper.findWaitAuditNewsList(findWaitAuditNewsListReq, areaIds);
        }

        if (!CollectionUtils.isEmpty(list)) {
            for (WaitAuditNewsVo item : list) {
                Integer currentStatus = item.getAuditStatus();
                if (OneLevelAuditConstant.DRAFT.equals(currentStatus)) {
                    item.setCurrentStatusStr("待提交");
                } else if (OneLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
                    item.setCurrentStatusStr("待审核");
                } else if (OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
                    item.setCurrentStatusStr("已同意");
                } else {
                    item.setCurrentStatusStr("已驳回");
                }
            }
        }

        return new DataList<WaitAuditNewsVo>(list, total);
    }

    /**
     * 审核镇级党务公开
     *
     * @param auditNewsReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean auditNewsByZJDW(AuditNewsReq auditNewsReq) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();

        //获取用户的角色 并判断是否是镇党建办主任
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        if (!userMaxRole.equals(SystemRoleConstant.Z_DJB_ZR)) {
            throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
        }

        //判断是审核通过还是不通过
        if (auditNewsReq.getAuditStatus().equals(OneLevelAuditConstant.CONSENT)) {
            newAuditMapper.auditSuccessNewsByZJDW(auditNewsReq);
            newAuditMapper.addAuditSuccessNewsByZJDWRecord(auditNewsReq, AuditConstant.WZ_ZJ_DW_AUDIT, dtUsersVo.getId());
        } else if (auditNewsReq.getAuditStatus().equals(OneLevelAuditConstant.FAIL)) {
            if (StringUtils.isBlank(auditNewsReq.getFailMsg())) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "请输入拒绝理由");
            }
            newAuditMapper.auditFailNewsByZJDW(auditNewsReq);
            newAuditMapper.addAuditFailNewsByZJDWRecord(auditNewsReq, AuditConstant.WZ_ZJ_DW_AUDIT, dtUsersVo.getId());
        }
        return Boolean.TRUE;
    }

    /**
     * 是否公开镇级党务公开
     *
     * @param openNewsReq
     * @return
     * @throws SzException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean openNewsByZJDW(OpenNewsReq openNewsReq) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();

        //获取用户的角色 并判断是否是镇党建办主任
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        if (!userMaxRole.equals(SystemRoleConstant.Z_DJB_ZR)) {
            throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
        }
        AxhFinanceLog axhFinanceLog = new AxhFinanceLog();

        //执行公开操作
        newAuditMapper.openNewsByZJDW(openNewsReq);
        newAuditMapper.openNewsByZJDWRecord(openNewsReq, AuditConstant.WZ_ZJ_DW_AUDIT);

        if (openNewsReq.getIsPublic().equals(1)) {
            axhFinanceLog.setRemark("发布");
            axhFinanceLog.setLogContent("【报表审核】用户" + dtUsersVo.getUsername() + "审核了该报表，状态为：已公开");
        } else {
            axhFinanceLog.setRemark("取消发布");
            axhFinanceLog.setLogContent("【报表审核】用户" + dtUsersVo.getUsername() + "审核了该报表，状态为：未公开");
        }
        axhFinanceLog.setFinanceId(openNewsReq.getId());
        axhFinanceLog.setCurrentFinanceStatus(OneLevelAuditConstant.PUBLIC);
        axhFinanceLog.setCommandUserId(dtUsersVo.getId());
        financeAuditMapper.addAuditLog(axhFinanceLog);

        return Boolean.TRUE;
    }

    /**
     * 是否公开镇级政务公开
     *
     * @param openNewsReq
     * @return
     * @throws SzException
     */
    @Transactional
    @Override
    public Boolean openNewsByZJZW(OpenNewsReq openNewsReq) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();

        //获取用户的角色 并判断是否是镇便民服务中心主任
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        if (!userMaxRole.equals(SystemRoleConstant.Z_BM_FW_ZX_ZR)) {
            throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
        }
        AxhFinanceLog axhFinanceLog = new AxhFinanceLog();

        //执行公开操作
        newAuditMapper.openNewsByZJZW(openNewsReq);
        newAuditMapper.openNewsByZJZWRecord(openNewsReq, AuditConstant.WZ_ZJ_ZW_AUDIT);

        if (openNewsReq.getIsPublic().equals(1)) {
            axhFinanceLog.setRemark("发布");
            axhFinanceLog.setLogContent("【报表审核】用户" + dtUsersVo.getUsername() + "审核了该报表，状态为：已公开");
        } else {
            axhFinanceLog.setRemark("取消发布");
            axhFinanceLog.setLogContent("【报表审核】用户" + dtUsersVo.getUsername() + "审核了该报表，状态为：未公开");
        }
        axhFinanceLog.setFinanceId(openNewsReq.getId());
        axhFinanceLog.setCurrentFinanceStatus(OneLevelAuditConstant.PUBLIC);
        axhFinanceLog.setCommandUserId(dtUsersVo.getId());
        financeAuditMapper.addAuditLog(axhFinanceLog);

        return Boolean.TRUE;
    }

    /**
     * 审核镇级政务公开
     *
     * @param auditNewsReq
     * @return
     */
    @Override
    public Boolean auditNewsByZJZW(AuditNewsReq auditNewsReq) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();

        //获取用户的角色 并判断是否是镇便民服务中心主任
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        if (!userMaxRole.equals(SystemRoleConstant.Z_BM_FW_ZX_ZR)) {
            throw new SzException(SystemCode.USER_JURISDICTION_ERROR, "暂无权限");
        }

        //判断是审核通过还是不通过
        if (auditNewsReq.getAuditStatus().equals(OneLevelAuditConstant.CONSENT)) {
            newAuditMapper.auditSuccessNewsByZJZW(auditNewsReq);
            newAuditMapper.addAuditSuccessNewsByZJZWRecord(auditNewsReq, AuditConstant.WZ_ZJ_ZW_AUDIT, dtUsersVo.getId());
        } else if (auditNewsReq.getAuditStatus().equals(OneLevelAuditConstant.FAIL)) {
            if (StringUtils.isBlank(auditNewsReq.getFailMsg())) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "请输入拒绝理由");
            }
            newAuditMapper.auditFailNewsByZJZW(auditNewsReq);
            newAuditMapper.addAuditFailNewsByZJZWRecord(auditNewsReq, AuditConstant.WZ_ZJ_ZW_AUDIT, dtUsersVo.getId());
        }
        return Boolean.TRUE;
    }

    /**
     * 通过审核文章
     *
     * @param agreePassNewsAuditReq 参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void agreePassNewsAudit(AgreePassNewsAuditReq agreePassNewsAuditReq) throws SzException {
        agreePassNewsAuditReq.format();

        DtNewsVo dtNewsVo = newAuditMapper.findNewById(agreePassNewsAuditReq.getId());
        if (null == dtNewsVo) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "数据异常~~~");
        }

        if (!(OneLevelAuditConstant.SUBMIT_AUDIT.equals(dtNewsVo.getAuditStatus()) || OneLevelAuditConstant.FAIL.equals(dtNewsVo.getAuditStatus()))) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "未提交~~~");
        }

        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();

        Integer contentType = dtNewsVo.getContentType();

        String areaIds = "";

        //镇级党务公开 镇党建办主任
        if (ContentConstant.ZJ_DW.equals(contentType)) {
            if (!SystemRoleConstant.Z_DJB_ZR.equals(role)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageTownId();
        }
        // 镇级政务公开 镇便民服务中心主任
        else if (ContentConstant.ZJ_ZW.equals(contentType)) {
            if (!SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageTownId();
        }
        // 村级党务公开 镇党建办主任 镇党建办成员
        else if (ContentConstant.CJ_DW.equals(contentType)) {
            if (!(SystemRoleConstant.Z_DJB_CY.equals(role) || SystemRoleConstant.Z_DJB_ZR.equals(role))) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageAreaId() + "," + user.findUserManageCommunityId();
        }

        // 村级党务公开 镇相关部门 镇便民服务中心主任
        else if (ContentConstant.CJ_CW.equals(contentType)) {
            if (!(SystemRoleConstant.Z_XG_BM.equals(role) || SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role))) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
            areaIds = user.findUserManageAreaId() + "," + user.findUserManageCommunityId();
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        //校验文章是否属于当前用户管辖范围
        if (!("," + areaIds + ",").contains("," + dtNewsVo.getFromToAreaId() + ",")) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        dtNewsVo.setAuditStatus(agreePassNewsAuditReq.getType().equals(1) ? OneLevelAuditConstant.CONSENT : OneLevelAuditConstant.FAIL);
        dtNewsVo.setFailMsg(agreePassNewsAuditReq.getType().equals(1) ? null : agreePassNewsAuditReq.getFailMsg());


        //修改文章审核状态
        newAuditMapper.updateNewAuditStatus(dtNewsVo);
        //修改文章审核记录状态
        newAuditMapper.updateNewAuditRecordStatus(agreePassNewsAuditReq.getAuditId(), dtNewsVo.getAuditStatus(), dtNewsVo.getFailMsg());

        AxhFinanceLogVo flag = new AxhFinanceLogVo();
        flag.setLogType(2);
        flag.setRemark(agreePassNewsAuditReq.getType().equals(1) ? "同意" : dtNewsVo.getFailMsg());
        flag.setFinanceId(dtNewsVo.getId());
        flag.setCommandUserId(user.getId());
        flag.setLogContent("【文章审核】用户 " + user.getUsername() + " " + flag.getRemark() + " 了文章 " + dtNewsVo.getTitle() + " 通过审核");
        flag.setCurrentFinanceStatus(dtNewsVo.getAuditStatus());
        reportFormsMapper.addFinanceLog(flag);
    }

    /**
     * 查询待审核文章数据
     *
     * @return 待审核文章数据
     */
    @Override
    public FindWaitAuditNewsSizeVo findWaitAuditNewsSize(Integer auditType) throws SzException {
        if (null == auditType) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }

        Integer contentType = null;
        if (auditType.equals(5)) {
            contentType = ContentConstant.ZJ_DW;
        } else if (auditType.equals(6)) {
            contentType = ContentConstant.ZJ_ZW;
        } else if (auditType.equals(7)) {
            contentType = ContentConstant.CJ_DW;
        } else if (auditType.equals(8)) {
            contentType = ContentConstant.CJ_CW;
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }

        String commissionSizeStr = OneLevelAuditConstant.SUBMIT_AUDIT + "";
        String doneSizeStr = OneLevelAuditConstant.CONSENT + "," + OneLevelAuditConstant.FAIL;


        FindWaitAuditNewsSizeVo flag = newAuditMapper.findWaitAuditNewsSize(contentType, commissionSizeStr, doneSizeStr);

        return flag;
    }

    /**
     * 查询文章审核日志
     *
     * @param id 文章id
     * @return 日志列表
     */
    @Override
    public List<AxhAuditFinanceLogVo> findNewsAuditLog(Integer id, Boolean bool) throws SzException {
        if (null == id || id < 1) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }

        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();
        if (!(SystemRoleConstant.Z_DJB_CY.equals(role) || SystemRoleConstant.Z_DJB_ZR.equals(role) || SystemRoleConstant.Z_XG_BM.equals(role) || SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role))) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
        }

        DtNewsVo dtNewsVo = newAuditMapper.findNewById(id);
        if (null == dtNewsVo) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "未查询到数据");
        }

        List<AxhAuditFinanceLogVo> list = new ArrayList<>();

        Integer currentStatus = dtNewsVo.getAuditStatus();

        //1为党务 2为村务/政务
        Integer type = null;
        if (dtNewsVo.getContentType().equals(ContentConstant.ZJ_DW) || dtNewsVo.getContentType().equals(ContentConstant.CJ_DW)) {
            type = 1;
        } else {
            type = 2;
        }

        //草稿
        if (OneLevelAuditConstant.DRAFT.equals(currentStatus)) {
            AxhAuditFinanceLogVo item1 = new AxhAuditFinanceLogVo();
            item1.setTitle("待提交");
            item1.setWait(1);
            item1.setWaitStr(item1.getTitle());

            AxhAuditFinanceLogVo item2 = new AxhAuditFinanceLogVo();
            item2.setTitle(type.equals(1) ? "镇党建办" : "镇相关部门");
            item2.setWait(1);


            list.add(item1);
            list.add(item2);
        }
        //已提交待审核
        else if (OneLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
            AxhAuditFinanceLogVo item1 = newAuditMapper.findNowsLog(id, OneLevelAuditConstant.SUBMIT_AUDIT, 2);
            item1.setTitle("已提交");
            item1.setWait(0);
            item1.setWaitStr(item1.getTitle());


            AxhAuditFinanceLogVo item2 = new AxhAuditFinanceLogVo();
            item2.setTitle(type.equals(1) ? "镇党建办" : "镇相关部门");
            item2.setWait(1);

            list.add(item1);
            list.add(item2);
        }
        //同意
        else if (OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
            AxhAuditFinanceLogVo item1 = newAuditMapper.findNowsLog(id, OneLevelAuditConstant.SUBMIT_AUDIT, 2);
            item1.setTitle("已提交");
            item1.setWait(0);
            item1.setWaitStr(item1.getTitle());


            AxhAuditFinanceLogVo item2 = newAuditMapper.findNowsLog(id, OneLevelAuditConstant.CONSENT, 2);
            item2.setWait(2);
            item2.setTitle(type.equals(1) ? "镇党建办" : "镇相关部门");


            list.add(item1);
            list.add(item2);
        }
        //拒绝
        else if (OneLevelAuditConstant.FAIL.equals(currentStatus)) {
            AxhAuditFinanceLogVo item1 = newAuditMapper.findNowsLog(id, OneLevelAuditConstant.SUBMIT_AUDIT, 2);
            item1.setTitle("已提交");
            item1.setWait(0);
            item1.setWaitStr(item1.getTitle());


            AxhAuditFinanceLogVo item2 = newAuditMapper.findNowsLog(id, OneLevelAuditConstant.FAIL, 2);
            item2.setWait(3);
            item2.setTitle(type.equals(1) ? "镇党建办" : "镇相关部门");


            list.add(item1);
            list.add(item2);
        }

        AxhAuditFinanceLogVo item3 = newAuditMapper.findNowsLog(id, OneLevelAuditConstant.PUBLIC, 2);
        if (null != item3) {
            item3.setTitle(item3.getRemark());
            item3.setWait("发布".equals(item3.getRemark()) ? 2 : 3);
            item3.setWaitStr("发布".equals(item3.getRemark()) ? "已发布" : "已下架");
        } else {
            item3 = new AxhAuditFinanceLogVo();
            item3.setWait(1);
            item3.setTitle("待发布");
            item3.setWaitStr("待发布");
        }
        list.add(item3);

        if (bool) {
            Collections.reverse(list);
        }
        return list;
    }

    /**
     * 通过id查询待审核文章对象
     *
     * @param id 文章id
     * @return 待审核文章对象
     */
    @Override
    public WaitAuditNewsVo findNewsVoById(Integer id) throws SzException {
        if (null == id || id < 1) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数异常");
        }


        WaitAuditNewsVo flag = newAuditMapper.findNewsVoById(id);

        if (null == flag) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有查询到数据");
        }
        DtUsersVo user = DtUsersVo.findCurrentUser();
        Integer role = user.getUserMaxRole();
        if (!user.getId().equals(flag.getCreateUserId())) {
            Integer contentType = flag.getContentType();
            String areaIds = "";
            if (ContentConstant.ZJ_DW.equals(contentType) || ContentConstant.ZJ_ZW.equals(contentType)) {
                areaIds = user.findUserManageTownId();
            } else if (ContentConstant.CJ_CW.equals(contentType) || ContentConstant.CJ_DW.equals(contentType)) {
                areaIds = user.findUserManageAreaId() + "," + user.findUserManageCommunityId();
            } else {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "数据异常");
            }

            if (!("," + areaIds + ",").contains("," + flag.getFromToAreaId() + ",")) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有操作权限~~~");
            }
        }


        Integer currentStatus = flag.getAuditStatus();
        if (OneLevelAuditConstant.DRAFT.equals(currentStatus)) {
            flag.setCurrentStatusStr("草稿");
        } else if (OneLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
            flag.setCurrentStatusStr("审核中");
        } else if (OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
            flag.setCurrentStatusStr("已通过");
        } else {
            flag.setCurrentStatusStr("已驳回");
        }


        FinanceBtnVo financeBtnVo = new FinanceBtnVo();
        financeBtnVo.setIsAgreePublicBtn(checkAgreePublicBtn(role, flag));
        financeBtnVo.setIsAgreeRefuseAuditBtn(checkAgreeRefuseAuditBtn(role, flag));
        flag.setFinanceBtnVo(financeBtnVo);

        flag.setAxhAuditFinanceLogVos(this.findNewsAuditLog(id, Boolean.FALSE));


        return flag;
    }

    /**
     * 查询分类列表
     *
     * @return 分类列表
     */
    @Override
    public List<BaseSelectVo> findClassifyList() {
        List<BaseSelectVo> flag = new ArrayList<>();
        flag.add(new BaseSelectVo(AddressConstant.CUN, "村"));
        flag.add(new BaseSelectVo(AddressConstant.SQ, "社区"));
        flag.add(new BaseSelectVo(AddressConstant.JW, "居委会"));

        List<BaseSelectVo> tem = newAuditMapper.findClassifyList();
        if (!CollectionUtils.isEmpty(tem)) {
            Map<Integer, List<BaseSelectVo>> map = tem.stream().collect(Collectors.groupingBy(BaseSelectVo::getPid));
            for (BaseSelectVo obj : flag) {
                obj.setChildren(map.get(obj.getValue()));
            }
        }

        return flag;
    }

    /**
     * 判断是否显示发布按钮
     *
     * @param role 角色
     * @param flag 待审核文章对象
     */
    private Boolean checkAgreePublicBtn(Integer role, WaitAuditNewsVo flag) {
        Integer contentType = flag.getContentType();
        Integer currentStatus = flag.getAuditStatus();

        //是否已通过
        if (!OneLevelAuditConstant.CONSENT.equals(currentStatus)) {
            return Boolean.FALSE;
        }

        if (ContentConstant.ZJ_DW.equals(contentType)) {
            if (SystemRoleConstant.Z_DJB_ZR.equals(role)) {
                return Boolean.TRUE;
            }
        } else if (ContentConstant.ZJ_ZW.equals(contentType)) {
            if (SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role)) {
                return Boolean.TRUE;
            }
        } else if (ContentConstant.CJ_DW.equals(contentType) || ContentConstant.CJ_CW.equals(contentType)) {
            if (SystemRoleConstant.CUN.equals(role) || SystemRoleConstant.SQ.equals(role)) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }


    /**
     * 判断是否显示同意拒绝按钮
     *
     * @param role 角色
     * @param flag 文章
     */
    private Boolean checkAgreeRefuseAuditBtn(Integer role, WaitAuditNewsVo flag) throws SzException {
        Integer contentType = flag.getContentType();
        Integer currentStatus = flag.getAuditStatus();
        //是否待审核
        if (!OneLevelAuditConstant.SUBMIT_AUDIT.equals(currentStatus)) {
            return Boolean.FALSE;
        }


        if (ContentConstant.ZJ_DW.equals(contentType)) {
            if (SystemRoleConstant.Z_DJB_ZR.equals(role)) {
                return Boolean.TRUE;
            }
        } else if (ContentConstant.ZJ_ZW.equals(contentType)) {
            if (SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role)) {
                return Boolean.TRUE;
            }
        } else if (ContentConstant.CJ_DW.equals(contentType)) {
            if (SystemRoleConstant.Z_DJB_ZR.equals(role) || SystemRoleConstant.Z_DJB_CY.equals(role)) {
                return Boolean.TRUE;
            }
        } else if (ContentConstant.CJ_CW.equals(contentType)) {
            if (SystemRoleConstant.Z_XG_BM.equals(role) || SystemRoleConstant.Z_BM_FW_ZX_ZR.equals(role)) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }


}
